#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_connect_clicked()
{
    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("student");
    if(!database.open())
    {
        qDebug() << "连接数据库失败" << "errcode: " << QSqlError::ConnectionError;
    }
    else
    {
        qDebug() << "连接数据库成功";
    }
}


void Widget::on_create_clicked()
{
    QSqlQuery query;
    QString sql = "CREATE TABLE IF NOT EXISTS stu(\
                    id INTEGER PRIMARY KEY,\
                    name TEXT,\
                    age INTEGER)";
    if(!query.exec(sql)){
        qDebug() << "创建表失败";
    }
    else{
        qDebug() << "创建表成功";
    }
}


void Widget::on_add_clicked()
{
    QSqlQuery query;
    QString sql = "INSERT INTO stu (id, name, age) VALUES (1, 'John Doe', 19)";


    if (!query.exec(sql)) {


        qDebug() << "Failed to insert data:" << query.lastError().text();


    }
}

